package com.example.base.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.system.domain.SysRoleApi;
import com.example.base.system.domain.vo.SysRoleApiVo;
import com.example.base.system.mapper.SysRoleApiMapper;
import com.example.base.system.service.SysRoleApiService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.cache.annotation.*;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.CacheConfig;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author X.I.O
 * @since 2024-04-10
 */
@Service
@Transactional
@CacheConfig(cacheNames = "SysRoleApiServiceImpl")
public class SysRoleApiServiceImpl extends ServiceImpl<SysRoleApiMapper, SysRoleApi> implements SysRoleApiService {

    @Cacheable(cacheNames = "sysroleapi",key = "#id")
    @Override
    public SysRoleApi getById(Serializable id){
        return super.getById(id);
    }

    @CacheEvict(cacheNames = "sysroleapi", key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @CacheEvict(cacheNames = "sysroleapi", key = "#sysroleapi.id")
    @Override
    public boolean updateById(SysRoleApi sysroleapi) {
        return super.updateById(sysroleapi);
    }

    @CachePut(cacheNames = "sysroleapi", key = "#sysroleapi.id")
    public SysRoleApi saveSysRoleApi(SysRoleApi sysroleapi) throws GlobalExcetion {
        super.save(sysroleapi);
        return sysroleapi;
    }

    @CacheEvict(cacheNames = "sysroleapi", key = "#sysroleapi.id")
    public SysRoleApi updateSysRoleApiById(SysRoleApi sysroleapi) throws GlobalExcetion{
        super.updateById(sysroleapi);
        return sysroleapi;
    }


    public SysRoleApi saveOrUpdateSysRoleApi(SysRoleApiVo sysroleapiVo) throws GlobalExcetion{
        SysRoleApi sysroleapi=new SysRoleApi();
        BeanUtils.copyProperties(sysroleapiVo,sysroleapi);
        if(sysroleapiVo.getId()!=null){
            SysRoleApi sysroleapiEntity=super.baseMapper.selectById(sysroleapiVo.getId());
            if(sysroleapiEntity==null){
                throw new GlobalExcetion(1,"数据不存在");
            }
            super.baseMapper.updateById(sysroleapi);
        }else{
            super.baseMapper.insert(sysroleapi);
        }
        return sysroleapi;
    }

    @Override
    public SysRoleApi getSysRoleApiByRoleIdAndApiId(String roleId, String apiId) {
        LambdaQueryWrapper<SysRoleApi> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRoleApi::getApiId,apiId);
        lambdaQueryWrapper.eq(SysRoleApi::getRoleId,roleId);
        return super.baseMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public List<SysRoleApi> getSysRoleApiListByRoleId(String roleId) {
        LambdaQueryWrapper<SysRoleApi> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRoleApi::getRoleId,roleId);
        return super.baseMapper.selectList(lambdaQueryWrapper);
    }


    public IPage<SysRoleApi> page(Integer page, Integer pageSize) throws GlobalExcetion{
        LambdaQueryWrapper<SysRoleApi> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        IPage<SysRoleApi> iPage=super.baseMapper.selectPage(new Page<>(page,pageSize),lambdaQueryWrapper);
        return iPage;
    }
}
